﻿using AutoMapper;
using MediatR;
using Traceability.Commont.Domains;
using Traceability.Commont.DTOs.CommontFiles;
using Traceability.Commont.Infrastructures.Interfaces;

namespace Traceability.Api.Applications.Permissionts
{
    public class PermissionListHander : IRequestHandler<PermissionsListCommand, Resqults<Permissions>>
    {
        private readonly IBateRepository<Permissions> permissions;
        private readonly IMapper mapper;

        public PermissionListHander(IBateRepository<Permissions> permissions, IMapper mapper)
        {
            this.permissions = permissions;
            this.mapper = mapper;
        }

        public async Task<Resqults<Permissions>> Handle(PermissionsListCommand request, CancellationToken cancellationToken)
        {
            Resqults<Permissions> res =new Resqults<Permissions>();
            try
            {
                var list = permissions.GetAll().ToList();
                if (!string.IsNullOrEmpty(request.permissName))
                {
                    list = list.Where(x => x.PermissionsName.Contains(request.permissName)).ToList();
                }
                if (list.Count > 0)
                {
                    res.Date = list;
                    res.Code = 1;
                    res.Messges = "查询成功";
                }
            }
            catch (Exception)
            {

                throw;
            }
            return res;
        }
    }
}
